﻿namespace CommonLibrary
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Net.Mail;
    using System.Configuration;

    public class MailHelper
    {
        private class Option
        {
            public string UserName { get; set; }
            public string Address { get; set; }
            public string Password { get; set; }
        }

        private Option option;
        public MailHelper()
        {
            this.option = LoadConfig();
        }

        public void SendEmail(string to, string subject, string message)
        {
            using (var client = new SmtpClient("mail.xk-soft.com")
                {
                    UseDefaultCredentials = true,
                    Credentials = new System.Net.NetworkCredential(option.UserName, option.Password)
                })
            {
                var mail = new MailMessage(
                    option.Address,
                    to,
                    subject,
                    message);
                mail.IsBodyHtml = true;
                client.Send(mail);
            }
        }

        private static Option LoadConfig()
        {            
            return new Option
            {
                UserName = ConfigurationManager.AppSettings["UserName"],
                Address = ConfigurationManager.AppSettings["Address"],
                Password = ConfigurationManager.AppSettings["Password"]
            };
        }
    }
}